package com.vict.config.h2;

import com.vict.config.ApplicationContextRegister;
import com.vict.config.Init;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

/**
 * 初始化h2数据库
 */
@Slf4j
@Service
@AutoConfigureAfter(DataSource.class) //DataSource创建完后才初始化此类
public class H2DataSourceConfig {
    @Autowired
    DataSource dataSource;

    @Autowired
    ApplicationContextRegister applicationContextRegister; //自定义注册器

    public  void init() throws Exception {
        log.info("初始化h2数据");

        if(Init.isInit){
            // 已经初始化
        }else{
            // Resource resource = applicationContextRegister.getResource("classpath:dbInit/dbInit.sql");
            // ScriptUtils.executeSqlScript(dataSource.getConnection(), resource);
        }
        log.info("初始化h2数据完成");
    }
}
